package com.zsl.controller.User;

import com.alibaba.fastjson.JSON;
import com.zsl.service.BookService;
import com.zsl.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "GetBookTable", urlPatterns = "/get/book/table")
public class GetBookTable extends HttpServlet {
    private final BookService bookService = new BookServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取数据
        String page_num = request.getParameter("page_num");
        String b_name_sstr = request.getParameter("b_name_sstr").equals("") ? null : request.getParameter("b_name_sstr");
        String c_name_sstr = request.getParameter("c_name_sstr").equals("") ? null : request.getParameter("c_name_sstr");

        System.out.println(page_num);
        System.out.println(b_name_sstr);
        System.out.println(c_name_sstr);

        Map<String, Object> res = new HashMap<>();
        //处理过程
        try {
            res = bookService.getBookTableByPage(page_num, b_name_sstr, c_name_sstr);
            res.put("code", 1);
            res.put("msg", "读取成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "未知错误");
        } finally {
            PrintWriter out = response.getWriter();
            out.write(JSON.toJSONString(res));
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
